﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Common.Helper;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Purchase;
using PeiYue.Model;
using PeiYue.Model.Dto.Purchase;
using PeiYue.Model.ERPModels;

namespace PeiYue.Api.Controllers.Purchase
{
    /// <summary>
    /// 账单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SupplierBillController : ControllerBase
    {
        readonly IShouldPayServices _shouldPayServices;
        readonly IPurchaseOrderDetail _purchaseOrderDetail;
        readonly ISupplierServices _supplierServices;
        readonly IUser _user;
        readonly IMapper _mapper;
        public SupplierBillController(IShouldPayServices shouldPayServices, IPurchaseOrderDetail purchaseOrderDetail,
           ISupplierServices supplierServices,
           IUser user, IMapper mapper)
        {
            _shouldPayServices = shouldPayServices;
            _purchaseOrderDetail = purchaseOrderDetail;
            _supplierServices = supplierServices;
            _user = user;
            this._mapper = mapper;
        }


        /// <summary>
        /// 获取账单列表
        /// </summary>
        /// <param name="querymodel"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<ShouldPayDTO>>> GetSuppBillList([FromQuery] SearchShouldPayDTODTO querymodel)
        {
            var supplierCode = (await _supplierServices.QueryById(_user.ResId))?.codec;
            Expression<Func<ErpShouldPay, bool>> ex = t => t.SupplierCode==supplierCode;
            if (querymodel.DeliveryDate.HasValue)
                ex = ex.And(t => t.DeliveryDate >= querymodel.DeliveryDate);
            else
                ex = ex.And(t => t.DeliveryDate >= DateTime.Now.AddDays(-90));//    .AddMonths(-3));;
            if (querymodel.DeliveryDateEnd.HasValue)
                ex = ex.And(t => t.DeliveryDate <= querymodel.DeliveryDateEnd);
            else
                ex = ex.And(t => t.DeliveryDate <= DateTime.Now.AddYears(100));
            var list = await _shouldPayServices.Query(ex);
            var dtoList = list.OrderByDescending(i => i.DeliveryDate).ThenBy(t=>t.PONo).ToList();
            List<ShouldPayDTO> dto = new List<ShouldPayDTO>();
            dto = _mapper.Map<List<ShouldPayDTO>>(dtoList);
            return new MessageModel<PageModel<ShouldPayDTO>>()
            { msg = "获取成功", success = true, response = new PageModel<ShouldPayDTO>() { data = dto} };
        }

        /// <summary>
        /// 获取账单明细
        /// </summary>
        /// <param name="querymodel"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<ERPPurchaseDetailDTO>>> GetSuppBillDeatil([FromQuery] SearchShouldPayDTODTO querymodel)
        {
            var list = await _purchaseOrderDetail.Query(i => i.OrderId == querymodel.PONo);
            List<ERPPurchaseDetailDTO> dto = new List<ERPPurchaseDetailDTO>();
            dto = _mapper.Map<List<ERPPurchaseDetailDTO>>(list);
            dto.ForEach(t => {
                if (string.IsNullOrWhiteSpace(t.CustomerPriority))
                {
                    t.CustomerPriority = "";
                }
                if (string.IsNullOrWhiteSpace(t.CustomerCode))
                {
                    t.CustomerCode = "";
                }
            });
            return new MessageModel<PageModel<ERPPurchaseDetailDTO>>()
            { msg = "获取成功", success = true, response = new PageModel<ERPPurchaseDetailDTO>() { data = dto } };
        }

        /// <summary>
        /// 获取供应商收货单
        /// </summary>
        /// <param name="supplierCode">供应商编号</param>
        /// <param name="deliveryDate">预计发货日期</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<ReceivingGoodsDTO>> GetReceivingGoodsList(string supplierCode, DateTime deliveryDate)
        {
            var list = await _purchaseOrderDetail.GetReceivingGoodsList(supplierCode, deliveryDate);
            return new MessageModel<ReceivingGoodsDTO>()
            { msg = "获取成功", success = true, response = list };
        }

    }
}
